//
// Created by LU on 2022/10/21.
//

#ifndef MYINI_INIFILE_H
#define MYINI_INIFILE_H
#include <string>
#include <map>
using namespace std;

class Value
{
public:
    Value();
    Value(bool value);
    Value(int value);
    Value(double value);
    Value(const char * value);
    Value(const string & value);

    // 运算符重载
    Value & operator = (bool value);
    Value & operator = (int value);
    Value & operator = (double value);
    Value & operator = (const char * value);
    Value & operator = (const string & value);

    // 类型转换
    operator bool();
    operator int();
    operator double ();
    operator string();
private:
    string m_value;
};

typedef std::map<string, Value> Section;

class IniFile
{
public:
    IniFile();
    IniFile(const string & filename);
    ~IniFile();
    bool load(const string & filename);

    Value & get(const string & section, const string &key);

    // 重载[] 实现 ini["server"]["ip"]
    Section & operator [] (const string & section)
    {
        return m_sections[section];
    }
    //
    void set(const string & section, const string &key, const Value & value);
    bool has(const string & section, const string &key);
    bool has(const string & section);

    void remove(const string & section, const string & key);
    void remove(const string & section);
    void clear();
    void show();
    bool save(const string & filename);
    string str();

private:
    static string trim(string s);
private:
    string m_filename;
    std::map<string, Section> m_sections;
};

#endif //MYINI_INIFILE_H
